package api

import (
	"blog/library"
	"github.com/astaxie/beego"
	"strconv"
)

type PneumoniaController struct {
	beego.Controller
}

func (p PneumoniaController) List() {
	provinceId := p.GetString("province_id", "")
	data, err := library.Get(library.PNEUMONIA + "?province_id=" + provinceId)
	if err != nil {
		beego.Error(err.Error())
	}
	//a:=p.GetString("callback","")
	p.Data["json"] = data["Data"]
	p.ServeJSON()
}

func (p PneumoniaController) GetProvinceData() {
	//获取数据
	data, err := library.Get(library.PNEUMONIA + "?province_id=0")
	_ = err
	var province []map[string]string
	for _, v := range data["Data"].([]interface{}) {
		name := v.(map[string]interface{})["name"].(string)
		value := v.(map[string]interface{})["confirmed_count"].(float64)
		confirmedCount := strconv.FormatFloat(value, 'f', -1, 64)
		if library.UnicodeIndex(name, "省") != -1 {
			nameRune := []rune(name)
			name = string(nameRune[0 : len(nameRune)-1])
		}
		if library.UnicodeIndex(name, "市") != -1 {
			nameRune := []rune(name)
			name = string(nameRune[0 : len(nameRune)-1])
		}
		if library.UnicodeIndex(name, "内蒙古") != -1 {
			nameRune := []rune(name)
			name = string(nameRune[0:3])
		} else if library.UnicodeIndex(name, "自治区") != -1 {
			nameRune := []rune(name)
			name = string(nameRune[0:2])
		}
		province = append(province, map[string]string{"name": name, "value": confirmedCount})
	}

	p.Data["json"] = province
	p.ServeJSON()
}
